package com.xl.utils.util;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import lombok.extern.slf4j.Slf4j;

import java.util.HashMap;
import java.util.Map;

/**
 * @Author xionglin
 * @Classname ResultUtil
 * @Description TODO
 * @Date 2019/5/30 15:42
 */
@Slf4j
public class ResultUtil {

    private static String SUCCESS_CODE = "0";

    private static String FAIL_CODE = "1";

    /**
     * @Description: 包装下返回数据
     * @Param: code 状态码
     * @Param: obj 数据主体 @Param: msg 信息说明
     * @Author: xionglin @Date: 2019/4/29 15:19
     * @return:
     */
    public static Map<String, Object> map(String code, Object obj, String msg) {
        Map<String, Object> map = new HashMap<>(8);
        map.put("code", code);
        map.put("data", obj);
        map.put("msg", msg);
        return map;
    }
    /**
     * @Description: 包装下返回数据
     * @Param: code 状态码
     * @Param: msg 信息说明
     * @Author: xionglin @Date: 2019/4/29 15:19
     * @return:
     */
    public static Map<String, Object> map(String code, String msg) {
        Map<String, Object> map = new HashMap<>(8);
        map.put("code", code);
        map.put("msg", msg);
        return map;
    }
    /**
     * @Description: 包装下返回成功数据
     * @Param: obj 数据主体
     * @Param: msg 信息说明
     * @Author: xionglin @Date: 2019/4/29 15:19
     * @return:
     */
    public static Map<String, Object> successMap(Object obj, String msg) {
        return map(SUCCESS_CODE, obj, msg);
    }
    /**
     * @Description: 包装下返回成功数据
     * @Param: obj 数据主体
     * @Author: xionglin @Date: 2019/4/29 15:19
     * @return:
     */
    public static Map<String, Object> successMap(Object obj) {
        return map(SUCCESS_CODE, obj, "操作成功!");
    }
    /**
     * @Description: 包装下返回成功数据
     * @Param: msg 信息说明
     * @Author: xionglin @Date: 2019/4/29 15:19
     * @return:
     */
    public static Map<String, Object> successMap(String msg) {
        return map(SUCCESS_CODE, msg);
    }

    /**
     * @Description: 包装下返回成功数据
     * @Author: xionglin @Date: 2019/4/29 15:19
     * @return:
     */
    public static Map<String, Object> successMap() {
        return map(SUCCESS_CODE, "操作成功!");
    }

    /**
     * @Description: 包装下返回失败数据
     * @Param: obj 数据主体
     * @Param: msg 信息说明
     * @Author: xionglin @Date: 2019/4/29 15:19
     * @return:
     */
    public static Map<String, Object> failMap(Object obj, String msg) {
        return map(FAIL_CODE, obj, msg);
    }

    /**
     * @Description: 包装下返回失败数据
     * @Param: msg 信息说明
     * @Author: xionglin @Date: 2019/4/29 15:19
     * @return:
     */
    public static Map<String, Object> failMap(String msg) {
        return map(FAIL_CODE, msg);
    }
    /**
     * @Description: 包装下返回失败数据
     * @Param: msg 信息说明
     * @Author: xionglin @Date: 2019/4/29 15:19
     * @return:
     */
    public static Map<String, Object> failMap(Object obj) {
        return map(FAIL_CODE, obj, "操作失败");
    }
    /**
     * @Description: 包装下返回失败数据
     * @Param: msg 信息说明
     * @Author: xionglin @Date: 2019/4/29 15:19
     * @return:
     */
    public static Map<String, Object> failMap() {
        return map(FAIL_CODE, "操作失败");
    }

    public static String ResultJsonStr(String code, String msg){
        String str = null;
        try {
            str = new ObjectMapper().writeValueAsString(map(code,msg));
        } catch (JsonProcessingException e) {
            log.error(e.getMessage());
        }
        return str;
    }


}
